package com.xixilala.simple_use;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;


/**
 *
 */
public class MyAppInstallStats extends BroadcastReceiver {
    public static final String INSTALL_PASKNAME = "package";
    /**
     *
     */
    public static String maskName;
    private static IntentFilter mScreenStatusFilter = null;
    private static BroadcastReceiver mScreenStatusReceiver;

    /**
     * 开启 广播 监听
     */
    public static final void addInstallAppReceiver(Context context) {
        if (context == null) {
            return;
        }
        mScreenStatusFilter = new IntentFilter();
        mScreenStatusFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        mScreenStatusFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        mScreenStatusFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        mScreenStatusFilter.addDataScheme(INSTALL_PASKNAME);
        mScreenStatusReceiver = new MyAppInstallStats();
        context.registerReceiver(mScreenStatusReceiver, mScreenStatusFilter);

    }

    /**
     * 关闭 广播 监听
     */
    public static final void closeInstallAppReceiver(Context context) {
        if (context == null) {
            return;
        }
        if (mScreenStatusReceiver == null) {
            return;
        }
        context.unregisterReceiver(mScreenStatusReceiver);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        Uri data = intent.getData();
        String packageName = data.toString();
        LogUtils.d("收到安装成功app的广播-》" + packageName);
        if (TextUtils.isEmpty(packageName)) {
            return;
        }
        if (!packageName.startsWith(INSTALL_PASKNAME)) {
            return;
        }
        packageName = packageName.replaceFirst(INSTALL_PASKNAME + ":", "");

        if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
            //已安装
            LogUtils.d("已安装-》" + packageName);

            SharePreferencesUtils.setParam(context, InstallerHelperService.KEY_APP_INSTALLED,false);
            SharePreferencesUtils.setParam(context, InstallerHelperService.KEY_CLIP_BOARD,false);

//            Intent goAppDetail = new Intent();
//            goAppDetail.setClassName(context,"com.miui.securitycenter");
//            context.startActivity(goAppDetail);
            PackageManager packageManager = context.getPackageManager(); // 启动安全 中心
            Intent intentForPackage = packageManager.getLaunchIntentForPackage("com.miui.securitycenter");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intentForPackage);

        } else if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            //已更新
            LogUtils.d("已更新-》" + packageName);

            PackageManager packageManager = context.getPackageManager(); // 启动安全 中心
            Intent intentForPackage = packageManager.getLaunchIntentForPackage("com.miui.securitycenter");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intentForPackage);
        } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
            //已卸载
            LogUtils.d("已卸载-》" + packageName);
        }
    }
}
